﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Dapper;
using FirebirdSql.Data.FirebirdClient;

namespace Moose.DBUtility.Dapper
{
    public class FirebirdDBOperator
    {
        private int CommadnTimeout = 5;

        private string sDatabase;
        public string Database
        {
            get { return sDatabase; }
            set { sDatabase = value; }
        }
        private string sUserID;
        public string UserID
        {
            get { return sUserID; }
            set { sUserID = value; }
        }
        private string sPassword;
        public string Password
        {
            get { return sPassword; }
            set { sPassword = value; }
        }
        private string sCharset;
        public string Charset
        {
            get { return sCharset; }
            set { sCharset = value; }
        }

        private FbConnectionStringBuilder ConnectionString;

        private string GetConnstr()
        {
            if (ConnectionString == null)
            {
                ConnectionString = new FbConnectionStringBuilder();
                ConnectionString.Database = sDatabase;// "Rabase.fdb";
                ConnectionString.UserID = sUserID;// "sysdba";
                ConnectionString.Password = sPassword;// "masterkey";
                ConnectionString.Charset = sCharset;// "NONE";
                ConnectionString.ServerType = FbServerType.Embedded;
            }

            return ConnectionString.ToString();
        }


        public T GetSingle<T>(string _sqlScript, object param = null)
        {
            using (FbConnection DBConnection = new FbConnection(GetConnstr()))
            {
                return DBConnection.QueryFirst<T>(_sqlScript, param: param, commandTimeout: CommadnTimeout);
            }
        }

        public List<T> GetAll<T>(string _sqlScript, object param = null)
        {
            using (FbConnection DBConnection = new FbConnection(GetConnstr()))
            {
                return DBConnection.Query<T>(_sqlScript, param: param, commandTimeout: CommadnTimeout).ToList();
            }
        }

        /// <summary>
        /// 事务 - 全SQL
        /// </summary>
        /// <param name="sqlarr"></param>
        /// <returns></returns>
        public int ExceuteTransaction(string[] sqlarr)
        {
            int _Result = 0;
            using (FbConnection DBConnection = new FbConnection(GetConnstr()))
            {
                using (var _Transaction = DBConnection.BeginTransaction())
                {
                    try
                    {
                        foreach (string sql in sqlarr)
                        {
                            _Result += DBConnection.Execute(sql, null, _Transaction);
                        }

                        _Transaction.Commit();
                        return _Result;
                    }
                    catch (Exception ex)
                    {
                        _Transaction.Rollback();
                        return 0;
                    }
                }
            }
        }


        /// <summary>
        /// 事务 - 带参数
        /// </summary>
        /// <param name="dic"></param>
        /// <returns></returns>
        public int ExcuteTransaction(Dictionary<string, object> dic)
        {
            int _Result;

            using (FbConnection DBConnection = new FbConnection(GetConnstr()))
            {
                using (var _Transaction = DBConnection.BeginTransaction())
                {
                    try
                    {
                        _Result = 0;
                        foreach (var sql in dic)
                        {
                            _Result += DBConnection.Execute(sql.Key, sql.Value, _Transaction);
                        }

                        _Transaction.Commit();
                        return _Result;
                    }
                    catch (Exception ex)
                    {
                        _Transaction.Rollback();
                        return 0;
                    }
                }
            }
        }

        public int Excute(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            using (FbConnection DBConnection = new FbConnection(GetConnstr()))
            {
                return DBConnection.Execute(sql, param, transaction, commandTimeout, commandType);
            }
        }
        public List<T> Query<T>(string sql, object param = null, IDbTransaction transaction = null, bool buffered = true, int? commandTimeout = null, CommandType? commandType = null)
        {
            using (FbConnection DBConnection = new FbConnection(GetConnstr()))
            {
                return DBConnection.Query<T>(sql, param, transaction, buffered, commandTimeout, commandType).ToList();
            }
        }

    }
}
